﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DM_TOOLS.Tools.GPSConvert
{
    public class GpsCoordinate
    {
        #region 地球参数
        private static readonly double pi = 3.1415926535897932384626;
        private static readonly double a = 6378245.0;
        private static readonly double ee = 0.00669342162296594323;
        #endregion
        #region 坐标转换公开方法
        /// <summary>
        /// 坐标类型转换
        /// </summary>
        public enum LBS_TYPE
        {
            Wgs84_To_Gcj02 = 1,
            Gcj02_To_Wgs84 = 2,
            Gcj02_To_Bd09 = 3,
            Bd09_To_Gcj02 = 4,
            Bd09_To_Wgs84 = 5,
            Wgs84_To_Bd09 = 6
        }
        /// <summary>
        /// 坐标类型
        /// </summary>
        public enum GPS_TYPE
        {
            [Description("地球坐标系:标准GPS")]
            Wgs84 = 1,
            [Description("火星坐标系:高德")]
            Gcj02 = 2,
            [Description("高德坐标转地名:百度")]
            Bd09 = 3
        }
        /// <summary>
        /// 坐标转换
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lon">经度</param>
        /// <param name="OldTyle">旧坐标系</param>
        /// <param name="NewType">新坐标系</param>
        /// <returns></returns>
        public static GPS GpsToNewGps(double lat, double lon, GPS_TYPE OldTyle, GPS_TYPE NewType)
        {
            int OldTyle_1 = (int)OldTyle;
            int NewType_1 = (int)NewType;
            GPS? gs = null;
            switch (OldTyle_1 + "" + NewType_1)
            {
                case "12":
                    gs = Wgs84_To_Gcj02(lat, lon);
                    break;
                case "21":
                    gs = Gcj02_To_Wgs84(lat, lon);
                    break;
                case "23":
                    gs = Gcj02_To_Bd09(lat, lon);
                    break;
                case "32":
                    gs = Bd09_To_Gcj02(lat, lon);
                    break;
                case "31":
                    gs = Bd09_To_Wgs84(lat, lon);
                    break;
                case "13":
                    gs = Wgs84_To_Bd09(lat, lon);
                    break;
                default:
                    gs = new GPS(lat, lon);
                    break;
            }
            return gs;
        }

        /// <summary>
        ///  坐标转换
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lon">经度</param>
        /// <param name="type">转换类型（enum LBS_TYPE）</param>
        /// <returns></returns>
        public static GPS GpsToNewGps(double lat, double lon, LBS_TYPE type)
        {
            /**
            * type=1;Wgs84 to 火星坐标系 (GCJ-02)
            * type=2;火星坐标系 (GCJ-02) to Wgs84 
            * type=3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)
            * type=4;(BD-09)-->火星坐标系 (GCJ-02) 
            * type=5;(BD-09)-->Wgs84
            * **/
            GPS? gs = null;
            switch (type)
            {
                case LBS_TYPE.Wgs84_To_Gcj02:
                    gs = Wgs84_To_Gcj02(lat, lon);
                    break;
                case LBS_TYPE.Gcj02_To_Wgs84:
                    gs = Gcj02_To_Wgs84(lat, lon);
                    break;
                case LBS_TYPE.Gcj02_To_Bd09:
                    gs = Gcj02_To_Bd09(lat, lon);
                    break;
                case LBS_TYPE.Bd09_To_Gcj02:
                    gs = Bd09_To_Gcj02(lat, lon);
                    break;
                case LBS_TYPE.Bd09_To_Wgs84:
                    gs = Bd09_To_Wgs84(lat, lon);
                    break;
                case LBS_TYPE.Wgs84_To_Bd09:
                    gs = Wgs84_To_Bd09(lat, lon);
                    break;
                default:
                    gs = new GPS(lat, lon);
                    break;
            }
            return gs;
        }
        /// <summary>
        /// 坐标转换
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lon">经度</param>
        /// <param name="type">[1;Wgs84 to 火星坐标系 (GCJ-02)][2;火星坐标系 (GCJ-02) to Wgs84 ][3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)][4;(BD-09)-->火星坐标系 (GCJ-02)][5;(BD-09)-->Wgs84]</param>
        /// <returns>GPS</returns>
        public static GPS GpsToNewGps(double lat, double lon, string type)
        {
            /**
            * type=1;Wgs84 to 火星坐标系 (GCJ-02)
            * type=2;火星坐标系 (GCJ-02) to Wgs84 
            * type=3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)
            * type=4;(BD-09)-->火星坐标系 (GCJ-02) 
            * type=5;(BD-09)-->Wgs84
            * **/
            GPS? gs = null;
            switch (type)
            {
                case "1":
                    gs = Wgs84_To_Gcj02(lat, lon);
                    break;
                case "2":
                    gs = Gcj02_To_Wgs84(lat, lon);
                    break;
                case "3":
                    gs = Gcj02_To_Bd09(lat, lon);
                    break;
                case "4":
                    gs = Bd09_To_Gcj02(lat, lon);
                    break;
                case "5":
                    gs = Bd09_To_Wgs84(lat, lon);
                    break;
                case "6":
                    gs = Wgs84_To_Bd09(lat, lon);
                    break;
                default:
                    gs = new GPS(lat, lon);
                    break;
            }
            return gs;
        }
        /// <summary>
        /// 坐标转换
        /// Wgs84 to 火星坐标系 (GCJ-02):当返回null是标识坐标
        /// </summary>
        /// <param name="lat">纬度</param>
        /// <param name="lon">经度</param>
        /// <param name="type">[1;Wgs84 to 火星坐标系 (GCJ-02)][2;火星坐标系 (GCJ-02) to Wgs84 ][3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)][4;(BD-09)-->火星坐标系 (GCJ-02)][5;(BD-09)-->Wgs84]</param>
        /// <returns>GPS</returns>
        public static GPS GpsToNewGps(double lat, double lon, int type)
        {
            /**
            * type=1;Wgs84 to 火星坐标系 (GCJ-02)
            * type=2;火星坐标系 (GCJ-02) to Wgs84 
            * type=3;火星坐标系 (GCJ-02) 与百度坐标系 (BD-09)
            * type=4;(BD-09)-->火星坐标系 (GCJ-02) 
            * type=5;(BD-09)-->Wgs84
            * **/
            GPS? gs = null;
            switch (type)
            {
                case 1:
                    gs = Wgs84_To_Gcj02(lat, lon);
                    break;
                case 2:
                    gs = Gcj02_To_Wgs84(lat, lon);
                    break;
                case 3:
                    gs = Gcj02_To_Bd09(lat, lon);
                    break;
                case 4:
                    gs = Bd09_To_Gcj02(lat, lon);
                    break;
                case 5:
                    gs = Bd09_To_Wgs84(lat, lon);
                    break;
                case 6:
                    gs = Wgs84_To_Bd09(lat, lon);
                    break;
                default:
                    gs = new GPS(lat, lon);
                    break;
            }
            return gs;
        }
        #endregion
        #region 坐标转化
        /// <summary>
        ///Wgs84==>>GCJ-02
        /// </summary>
        private static GPS Wgs84_To_Gcj02(double lat, double lon)
        {
            if (OutOfChina(lat, lon))
            {
                return new GPS(lat, lon);
            }
            double dLat = transformLat(lon - 105.0, lat - 35.0);
            double dLon = transformLon(lon - 105.0, lat - 35.0);
            double radLat = lat / 180.0 * pi;
            double magic = Math.Sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.Sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
            double mgLat = lat + dLat;
            double mgLon = lon + dLon;
            return new GPS(mgLat, mgLon);
        }
        /// <summary>
        ///  GCJ-02==>>Wgs84
        /// </summary>
        private static GPS Gcj02_To_Wgs84(double lat, double lon)
        {
            GPS gps = transform(lat, lon);
            double lontitude = lon * 2 - gps._wgLat;
            double latitude = lat * 2 - gps._wgLon;
            return new GPS(latitude, lontitude);
        }
        /// <summary>
        /// GCJ-02==>>BD-09
        /// </summary>
        private static GPS Gcj02_To_Bd09(double gg_lat, double gg_lon)
        {
            if (OutOfChina(gg_lat, gg_lon))
            {
                return new GPS(gg_lat, gg_lon);
            }
            double x = gg_lon, y = gg_lat;
            double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * pi);
            double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * pi);
            double bd_lon = z * Math.Cos(theta) + 0.0065;
            double bd_lat = z * Math.Sin(theta) + 0.006;
            return new GPS(bd_lat, bd_lon);
        }
        /// <summary>
        /// BD-09==>>GCJ-02
        /// </summary>
        private static GPS Bd09_To_Gcj02(double bd_lat, double bd_lon)
        {
            double x = bd_lon - 0.0065, y = bd_lat - 0.006;
            double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * pi);
            double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * pi);
            double gg_lon = z * Math.Cos(theta);
            double gg_lat = z * Math.Sin(theta);
            return new GPS(gg_lat, gg_lon);
        }
        /// <summary>
        /// (BD-09)==>>Wgs84
        /// </summary>
        private static GPS Bd09_To_Wgs84(double bd_lat, double bd_lon)
        {

            GPS gcj02 = Bd09_To_Gcj02(bd_lat, bd_lon);
            GPS map84 = Gcj02_To_Wgs84(gcj02._wgLat, gcj02._wgLon);
            return map84;
        }
        /// <summary>
        /// Wgs84==>>(BD-09)
        /// </summary>
        /// <param name="bd_lat"></param>
        /// <param name="bd_lon"></param>
        /// <returns></returns>
        private static GPS Wgs84_To_Bd09(double bd_lat, double bd_lon)
        {

            GPS gcj02 = Wgs84_To_Gcj02(bd_lat, bd_lon);
            GPS map84 = Gcj02_To_Bd09(gcj02._wgLat, gcj02._wgLon);
            return map84;
        }
        #endregion
        #region 公共方法
        /// <summary>
        /// 是否在中国内
        /// </summary>
        /// <param name="lat"></param>
        /// <param name="lon"></param>
        /// <returns></returns>
        public static bool OutOfChina(double lat, double lon)
        {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            if (lat < 0.8293 || lat > 55.8271)
                return true;
            return false;
        }
        private static GPS transform(double lat, double lon)
        {
            if (OutOfChina(lat, lon))
            {
                return new GPS(lat, lon);
            }
            double dLat = transformLat(lon - 105.0, lat - 35.0);
            double dLon = transformLon(lon - 105.0, lat - 35.0);
            double radLat = lat / 180.0 * pi;
            double magic = Math.Sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.Sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
            double mgLat = lat + dLat;
            double mgLon = lon + dLon;
            return new GPS(mgLat, mgLon);
        }
        private static double transformLat(double x, double y)
        {
            double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                    + 0.2 * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
            ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
            return ret;
        }
        private static double transformLon(double x, double y)
        {
            double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                    * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
            ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0
                    * pi)) * 2.0 / 3.0;
            return ret;
        }
    }
    #endregion
}
